package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyTravel;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.StrategyTravelQuery;
import cn.wolfcode.trip.base.service.IStrategyTravelService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 大家都在看
 */
@Controller
@RequestMapping("/strategyTravel")
public class StrategyTravelController {
    @Autowired
    private IStrategyTravelService strategyTravelService;
    @Autowired
    private ITravelService travelService;
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")StrategyTravelQuery qo,Model model){
        if(qo.getStrategyId()!=null){
            //查询大家都在看的游记
            List<Travel> travelList=travelService.queryStrategyTravel(qo);
            model.addAttribute("travels",travelList);
        }
         return "strategy/travel";
    }

    /**
     * 插入 大家都在看 的游记
     * @return
     */
    @RequestMapping("/insertRelation")
    public String insertRelation(StrategyTravel st){
        strategyTravelService.insertRelation(st);
        return "redirect:/strategyTravel/list.do";
    }

    @RequestMapping("/deleteRelation")
    @ResponseBody
    public JSONResult deleteRelation(Long strategyId,Long travelId){
        JSONResult jsonResult=new JSONResult();
        try {
            strategyTravelService.deleteRelation(strategyId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("error");
        }
        return jsonResult;
    }
}
